﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Vimeo
{
    public static class VimeoExtensions
    {
        #region XElement

        public static VimeoGroup ToVimeoGroup(this XElement element)
        {
            return new VimeoGroup
            {
                Id = Convert.ToInt32(element.Element("id").Value),
                Url = element.Element("url").Value,
                Name = element.Element("name").Value,
                Description = element.Element("description").Value,
                Logo = element.Element("logo").Value,
                Thumbnail = element.Element("thumbnail").Value,
                CreatedOn = DateTime.Parse(element.Element("created_on").Value),
                CreatorId = Convert.ToInt32(element.Element("creator_id").Value),
                CreatorDisplayName = element.Element("creator_display_name").Value,
                CreatorUrl = element.Element("creator_url").Value,
                TotalMembers = Convert.ToInt32(element.Element("total_members").Value),
                TotalVideos = Convert.ToInt32(element.Element("total_videos").Value),
                TotalForumTopics = Convert.ToInt32(element.Element("total_forum_topics").Value),
                TotalEvents = Convert.ToInt32(element.Element("total_events").Value),
                TotalUpcomingEvents = Convert.ToInt32(element.Element("total_upcoming_events").Value)
            };
        }

        public static VimeoAlbum ToVimeoAlbum(this XElement element)
        {
            return new VimeoAlbum
            {
                Id = Convert.ToInt32(element.Element("id").Value),
                Url = element.Element("url").Value,
                Title = element.Element("title").Value,
                Description = element.Element("description").Value,
                ThumbnailSmall = element.Element("thumbnail_small").Value,
                ThumbnailMedium = element.Element("thumbnail_medium").Value,
                ThumbnailLarge = element.Element("thumbnail_large").Value,
                CreatedOn = DateTime.Parse(element.Element("created_on").Value),
                LastModifiedOn = DateTime.Parse(element.Element("last_modified").Value),
                TotalVideos = Convert.ToInt32(element.Element("total_videos").Value)
            };
        }

        public static VimeoChannel ToVimeoChannel(this XElement element)
        {
            return new VimeoChannel
            {
                Id = Convert.ToInt32(element.Element("id").Value),
                Url = element.Element("url").Value,
                Name = element.Element("name").Value,
                Description = element.Element("description").Value,
                Logo = element.Element("logo").Value,
                Rss = element.Element("rss").Value,
                CreatedOn = DateTime.Parse(element.Element("created_on").Value),
                CreatorId = Convert.ToInt32(element.Element("creator_id").Value),
                CreatorDisplayName = element.Element("creator_display_name").Value,
                CreatorUrl = element.Element("creator_url").Value,
                TotalVideos = Convert.ToInt32(element.Element("total_videos").Value),
                TotalSubscribers = Convert.ToInt32(element.Element("total_subscribers").Value)
            };
        }

        public static VimeoVideo ToVimeoVideo(this XElement element)
        {
            return new VimeoVideo
            {
                Id = Convert.ToInt32(element.Element("id").Value),
                Url = element.Element("url").Value,
                Title = element.Element("title").Value,
                Description = element.Element("description").Value,
                ThumbnailSmall = element.Element("thumbnail_small").Value,
                ThumbnailMedium = element.Element("thumbnail_medium").Value,
                ThumbnailLarge = element.Element("thumbnail_large").Value,
                Duration = Convert.ToInt32(element.Element("duration").Value),
                Width = Convert.ToInt32(element.Element("width").Value),
                Height = Convert.ToInt32(element.Element("height").Value),
                UploadDate = DateTime.Parse(element.Element("upload_date").Value),
                UserName = element.Element("user_name").Value,
                UserUrl = element.Element("user_url").Value,
                UserPortraitSmall = element.Element("user_portrait_small").Value,
                UserPortraitMedium = element.Element("user_portrait_medium").Value,
                UserPortraitLarge = element.Element("user_portrait_large").Value,
                StatsNumberOfLikes = Convert.ToInt32(element.Element("stats_number_of_likes").Value),
                StatsNumberOfPlays = Convert.ToInt32(element.Element("stats_number_of_plays").Value),
                StatsNumberOfComments = Convert.ToInt32(element.Element("stats_number_of_comments").Value),
                Tags = element.Element("tags").Value
            };
        }

        #endregion

        #region UMP

        public static MediaItem ToMediaItem(this VimeoGroup group)
        {
            return new MediaItem
            {
                Id = group.Id.ToString(),
                Title = group.Name,
                Icon = UmbracoIcons.FolderOpen,
                HasChildren = group.TotalVideos > 0,
                PreviewImageUrl = "",
                Selectable = false
            };
        }

        public static MediaItem ToMediaItem(this VimeoChannel channel)
        {
            return new MediaItem
            {
                Id = channel.Id.ToString(),
                Title = channel.Name,
                Icon = UmbracoIcons.FolderOpen,
                HasChildren = channel.TotalVideos > 0,
                PreviewImageUrl = "",
                Selectable = false
            };
        }

        public static MediaItem ToMediaItem(this VimeoAlbum album)
        {
            return new MediaItem
            {
                Id = album.Id.ToString(),
                Title = album.Title,
                Icon = UmbracoIcons.FolderOpen,
                HasChildren = album.TotalVideos > 0,
                PreviewImageUrl = "",
                Selectable = false
            };
        }

        public static MediaItem ToMediaItem(this VimeoVideo video)
        {
            var mi = new MediaItem
            {
                Id = video.Id.ToString(),
                Title = video.Title,
                Icon = "vimeoMedia.png",
                HasChildren = false,
                PreviewImageUrl = video.ThumbnailSmall,
                Selectable = true
            };

            var t = TimeSpan.FromSeconds(video.Duration);
            var duration = string.Format("{0:D2}:{1:D2}",
                                    t.Minutes,
                                    t.Seconds);

            mi.MetaData.Add("Description", video.Description);
            mi.MetaData.Add("Duration", duration);
            mi.MetaData.Add("Width", video.Width.ToString());
            mi.MetaData.Add("Height", video.Height.ToString());
            mi.MetaData.Add("Tags", video.Tags);
            mi.MetaData.Add("Upload Date", video.UploadDate.ToShortDateString());

            return mi;
        }

        #endregion
    }
}